//============================================================================================
/**
 * @file	fieldobj_bong.c
 * @author	NozomuSaito
 * @date	2008.06.04
 * @brief	フィールドOBJ ボングリ
 */
//============================================================================================

#include "common.h"
#include "fieldsys.h"
#include "map_tool.h"
#include "fieldobj.h"
#include "field_effect.h"
#include "bong_sys.h"

//--------------------------------------------------------------
///	MOVE_SEED_WORK構造体
//--------------------------------------------------------------
typedef struct
{
	u16 state;
	u16 harvest_flag;
}MOVE_BONG_WORK;

#define MOVE_BONG_WORK_SIZE (sizeof(MOVE_BONG_WORK))

//--------------------------------------------------------------
//	DRAW_BONG_WORK構造体
//--------------------------------------------------------------
typedef struct
{
	int DrawCode;
	int DrawState;
	BLACT_WORK_PTR act;
	FIELD_OBJ_BLACTANM_PUSH act_push;
}DRAW_BONG_WORK;

#define DRAW_BONG_WORK_SIZE (sizeof(DRAW_BONG_WORK))

static int GetBonguriOBJCode( const int inKind, const int inState );

//--------------------------------------------------------------
/**
 * OBJコードがボングリかチェック
 * @param	inCode	HERO等
 *
 * @retval	int		TRUE=ボングリ
 */
//--------------------------------------------------------------
int FieldOBJ_CheckOBJCodeBonguri( const int inCode )
{
	if( (inCode == BONGURI01) ||
		(inCode == BONGURI02) ||
		(inCode == BONGURI03) ||
		(inCode == BONGURI04) ||
		(inCode == BONGURI05) ||
		(inCode == BONGURI06) ||
		(inCode == BONGURI07) ){
		return( TRUE );
	}
	return( FALSE );
}

//==============================================================================
//	ボングリ動作
//==============================================================================
#if 0
//--------------------------------------------------------------
/**
 * MV_SEED　初期化
 * @param	fldobj	FIELD_OBJ_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
void FieldOBJ_MoveBong_Init( FIELD_OBJ_PTR fldobj )
{
	MOVE_BONG_WORK *work;

	work = FieldOBJ_MoveProcWorkInit( fldobj, MOVE_BONG_WORK_SIZE );
	work->state = BONG_STAT_NOTHING;
}

//--------------------------------------------------------------
/**
 * MV_SEED　動作
 * @param	fldobj	FIELD_OBJ_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
void FieldOBJ_MoveBong_Move( FIELD_OBJ_PTR fldobj )
{
	MOVE_BONG_WORK *work;

	work = FieldOBJ_MoveProcWorkGet( fldobj );
	work->state = BongSys_GetBongStatus( FieldOBJ_FieldSysWorkGet(fldobj), fldobj );
}

//--------------------------------------------------------------
/**
 * MV_SEED　削除
 * @param	fldobj	FIELD_OBJ_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
void FieldOBJ_MoveBong_Delete( FIELD_OBJ_PTR fldobj )
{
}
#endif

//==============================================================================
//	ボングリ　描画
//==============================================================================
//--------------------------------------------------------------
/**
 * ボングリ描画　初期化
 * @param	fldobj	FIELD_OBJ_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
void FieldOBJ_DrawBong_Init( FIELD_OBJ_PTR fldobj )
{
	DRAW_BONG_WORK *work = FieldOBJ_DrawProcWorkInit( fldobj, DRAW_BONG_WORK_SIZE );
	work->DrawCode = BONGURI;
	work->DrawState = BONG_STAT_NOTHING;

	FieldOBJ_DrawProcCall( fldobj );
}

//--------------------------------------------------------------
/**
 * ボングリ描画　描画
 * @param	fldobj	FIELD_OBJ_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
void FieldOBJ_DrawBong_Draw( FIELD_OBJ_PTR fldobj )
{
	MOVE_BONG_WORK *move = FieldOBJ_MoveProcWorkGet( fldobj );
	DRAW_BONG_WORK *draw = FieldOBJ_DrawProcWorkGet( fldobj );

	u32 new_state = BongSys_GetBongStatus( FieldOBJ_FieldSysWorkGet(fldobj), fldobj );

	if( FieldOBJ_BlActAddPracFlagCheck(fldobj) == TRUE ){			//追加中
		return;
	}

	//アクターまだないとき
	if (draw->act == NULL){
		draw->DrawCode = GetBonguriOBJCode(						//新しい木の実OBJコード取得
				BongSys_GetBongType(FieldOBJ_FieldSysWorkGet(fldobj),fldobj),
				new_state );
		FieldOBJ_OBJCodeSet( fldobj, draw->DrawCode );
		FieldOBJ_BlActAddRegularGuestCode( fldobj, &draw->act, draw->DrawCode );
	}else{
		if( new_state != draw->DrawState ){							//更新
			FieldOBJ_BlActDeleteCodeCancel( fldobj, &draw->act, draw->DrawCode );

			draw->DrawCode = GetBonguriOBJCode(						//新しい木の実OBJコード取得
					BongSys_GetBongType(FieldOBJ_FieldSysWorkGet(fldobj),fldobj),
					new_state );

			FieldOBJ_OBJCodeSet( fldobj, draw->DrawCode );

			if( draw->DrawCode != OBJCODEMAX ){						//表示すべき実がある
				if( new_state == BONG_STAT_FRUIT ){		//実が生った
					FE_FldOBJSeedEff_Add( fldobj );
				}
				FieldOBJ_BlActAddRegularGuestCode( fldobj, &draw->act, draw->DrawCode );
			}else{
				GF_ASSERT(0);
			}
			move->harvest_flag = FALSE;
		}
	}

	draw->DrawState = new_state;

	if( FieldOBJ_BlActAddPracFlagCheck(fldobj) == TRUE ){			//追加中
		return;
	}

	if( draw->act != NULL ){
		FieldOBJ_BlActPosSet( fldobj, draw->act );

		if( FieldOBJ_DrawPauseCheck(fldobj) == FALSE ){
			int state;
			state = FieldOBJ_DrawStatusGet( fldobj );
			switch( state ){
			case 0:
				if( BLACT_AnmOffsGet(draw->act) != 0 ){
					BLACT_AnmOffsChg( draw->act, 0 );
					BLACT_AnmFrameSetOffs( draw->act, 0 );
				}
				break;
			case 1:
				if( BLACT_AnmOffsGet(draw->act) != 1 ){
					BLACT_AnmOffsChg( draw->act, 1 );
					BLACT_AnmFrameSetOffs( draw->act, 0 );
				}
				break;
			case 2:
				if( BLACT_AnmOffsGet(draw->act) != 2 ){
					BLACT_AnmOffsChg( draw->act, 2 );
					BLACT_AnmFrameSetOffs( draw->act, 0 );
				}
				break;
			default:
				GF_ASSERT(0);
				if( BLACT_AnmOffsGet(draw->act) != 0 ){
					BLACT_AnmOffsChg( draw->act, 0 );
					BLACT_AnmFrameSetOffs( draw->act, 0 );
				}
			}

			BLACT_AnmFrameChg( draw->act, BLACTANM_FRAME_1 );
		}

		FieldOBJ_BlActFlagVanishSet( fldobj, draw->act );
	}
}

//--------------------------------------------------------------
/**
 * ボングリ描画　削除
 * @param	fldobj	FIELD_OBJ_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
void FieldOBJ_DrawBong_Delete( FIELD_OBJ_PTR fldobj )
{
	DRAW_BONG_WORK *work = FieldOBJ_DrawProcWorkGet( fldobj );
	FieldOBJ_BlActDeleteCodeCancel( fldobj, &work->act, work->DrawCode );
}

//--------------------------------------------------------------
/**
 * ボングリ描画　退避
 * @param	fldobj	FIELD_OBJ_PTR
 * @retval	nothing
 */
//--------------------------------------------------------------
void FieldOBJ_DrawBong_Push( FIELD_OBJ_PTR fldobj )
{
	DRAW_BONG_WORK *work = FieldOBJ_DrawProcWorkGet( fldobj );

	if( work->act != NULL ){
		FieldOBJ_BlActAnmPush( work->act, &work->act_push );
	}

	FieldOBJ_BlActDeleteCodeCancel( fldobj, &work->act, work->DrawCode );
	FieldOBJ_StatusBit_ON( fldobj, FLDOBJ_STA_BIT_DRAW_PUSH );
}

//--------------------------------------------------------------
/**
 * ボングリ描画　復帰
 * 退避した情報を元に再描画。
 * @param	fldobj		FIELD_OBJ_PTR
 * @retval	int			TRUE=初期化成功
 */
//--------------------------------------------------------------
void FieldOBJ_DrawBong_Pop( FIELD_OBJ_PTR fldobj )
{
	DRAW_BONG_WORK *work = FieldOBJ_DrawProcWorkGet( fldobj );

	if( FieldOBJ_BlActAddPracFlagCheck(fldobj) == TRUE ){
		return;
	}

	if( work->act == NULL ){
		if( work->DrawCode != OBJCODEMAX ){
			FieldOBJ_BlActAddRegularGuestCode( fldobj, &work->act, work->DrawCode );
		}else{
			GF_ASSERT(0);
/**
			FieldOBJ_StatusBit_OFF( fldobj, FLDOBJ_STA_BIT_DRAW_PUSH );
*/
		}

///		FE_FldOBJSeedGround_Add( fldobj );
	}

	if( work->act != NULL ){
		FieldOBJ_BlActAnmPop( work->act, &work->act_push );
		FieldOBJ_BlActPosSet( fldobj, work->act );
		FieldOBJ_StatusBit_OFF( fldobj, FLDOBJ_STA_BIT_DRAW_PUSH );
	}
}

//--------------------------------------------------------------
/**
 * ボングリアクターワーク取得
 * @param	fldobj		FIELD_OBJ_PTR
 * @retval	BLACT_WORK_PTR
 */
//--------------------------------------------------------------
BLACT_WORK_PTR FieldOBJ_BongGetBlActPtr( FIELD_OBJ_PTR fldobj )
{
	DRAW_BONG_WORK *work;
	if( FieldOBJ_BlActAddPracFlagCheck(fldobj) == TRUE ){
		GF_ASSERT(0)
		return NULL;
	}

	work = FieldOBJ_DrawProcWorkGet( fldobj );
	return work->act;
}

//--------------------------------------------------------------
/**
 * ボングリ種類、実の有無からOBJコード取得
 * @param	inKind		種類
 * @param	inState		生っているかどうかのステート
 * @retval	int			BONGURI00等 OBJCODEMAX=無し
 */
//--------------------------------------------------------------
static int GetBonguriOBJCode( const int inKind, const int inState )
{

	if (inState == BONG_STAT_NOTHING){
		return BONGURI;		//実なし
	}else{
		GF_ASSERT(inKind != 0);
		return DATA_OBJCodeBongDataTbl[inKind];
	}
}